﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class SubstituteSku : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlSubUom;
        protected DropDownList ddlUom;
        protected HtmlForm F2064;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnSubstituteSkuID;
        protected HtmlInputHidden hdnType;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblPack;
        protected Label lblQty;
        protected Label lblSeqNo;
        protected Label lblSkuID;
        protected Label lblSkuMainUomQty;
        protected Label lblSubPack;
        protected Label lblSubQty;
        protected Label lblSubSku;
        protected Label lblSubSkuMainSkuQty;
        protected Label lblSubstituteSku;
        protected Label lblSubUom;
        protected Label lblUdf1;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf4;
        protected Label lblUdf5;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtOwnerID;
        protected TextBox txtPack;
        protected TextBox txtQty;
        protected TextBox txtSeqNo;
        protected TextBox txtSkuID;
        protected TextBox txtSkuMainUomQty;
        protected TextBox txtSubPack;
        protected TextBox txtSubQty;
        protected TextBox txtSubSkuID;
        protected TextBox txtSubSkuMainSkuQty;
        protected TextBox txtUdf1;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;

        private void BindData2UomList(string packID, string strFlage)
        {
            try
            {
                if (strFlage == "1")
                {
                    base.BindUomList2DDL(this.ddlUom, packID);
                }
                else
                {
                    base.BindUomList2DDL(this.ddlSubUom, packID);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_022") + ex.Message;
                this.txtPack.Focus();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Sku.aspx?from={0}&currTab={1}&ID={2}&ID2={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "4", base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()) }), true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSubSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_020");
            }
            else if (string.IsNullOrEmpty(this.txtQty.Text.Trim()))
            {
                this.lblMsg.Text = "请输入原货品的替代数量!";
            }
            else if (string.IsNullOrEmpty(this.txtSubQty.Text.Trim()))
            {
                this.lblMsg.Text = "请输入替代货品的替代数量!";
            }
            else
            {
                bool success = false;
                try
                {
                    SubstituteSkuInfo substituteSkuInfo = new SubstituteSkuInfo {
                        OwnerID = this.txtOwnerID.Text.Trim(),
                        SkuID = this.txtSkuID.Text.Trim(),
                        PackID = this.txtPack.Text.Trim(),
                        UomID = this.ddlUom.SelectedValue,
                        Qty = decimal.Parse((this.txtQty.Text.Trim() == "") ? "1" : this.txtQty.Text.Trim()),
                        UomQty = decimal.Parse((this.txtSkuMainUomQty.Text.Trim() == "") ? "1" : this.txtSkuMainUomQty.Text.Trim()),
                        SeqNo = int.Parse(this.txtSeqNo.Text.Trim()),
                        SubstituteSkuID = this.txtSubSkuID.Text.Trim(),
                        SubPackID = this.txtSubPack.Text.Trim(),
                        SubUom = this.ddlSubUom.SelectedValue,
                        SubQty = decimal.Parse((this.txtSubQty.Text.Trim() == "") ? "1" : this.txtSubQty.Text.Trim()),
                        SubUomQty = decimal.Parse((this.txtSubSkuMainSkuQty.Text.Trim() == "") ? "1" : this.txtSubSkuMainSkuQty.Text.Trim()),
                        Udf1 = this.txtUdf1.Text.Trim(),
                        Udf2 = this.txtUdf2.Text.Trim(),
                        Udf3 = this.txtUdf3.Text.Trim(),
                        Udf4 = this.txtUdf4.Text.Trim(),
                        Udf5 = this.txtUdf5.Text.Trim(),
                        IsActive = this.chkIsActive.Checked ? "Y" : "N",
                        CreatedBy = base.UserInfo.UserID
                    };
                    if (this.hdnType.Value == "Edit")
                    {
                        substituteSkuInfo.UpdatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(substituteSkuInfo, "SUBSTITUTE_SKU")))
                        {
                            success = true;
                        }
                    }
                    else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(substituteSkuInfo, "SUBSTITUTE_SKU")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2110_021") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("Sku.aspx?from={0}&currTab={1}&ID={2}&ID2={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "4", base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()) }));
                }
            }
        }

        private void LoadData()
        {
            this.txtQty.Text = base.GetPreferenceDigit(1);
            this.txtSubQty.Text = base.GetPreferenceDigit(1);
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"].ToString()))
            {
                string from = base.Request.QueryString["from"].ToString();
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"].ToString()))
            {
                string ownerID = base.Request.QueryString["ID"].ToString();
                this.txtOwnerID.Text = ownerID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID2"].ToString()))
            {
                string skuID = base.Request.QueryString["ID2"].ToString();
                this.txtSkuID.Text = skuID;
                this.txtSkuID_TextChanged(null, null);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"].ToString()))
            {
                string seqNo = base.Request.QueryString["SeqNO"].ToString();
                this.txtSeqNo.Text = seqNo;
            }
            if (base.Request.QueryString["SUBSTITUTE_SKU_ID"] != null)
            {
                this.hdnSubstituteSkuID.Value = base.Request.QueryString["SUBSTITUTE_SKU_ID"].ToString();
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetSubStituteSku();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSubStituteSku()
        {
            SubstituteSkuInfo substituteSku = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtOwnerID.Text.Trim(), this.txtSkuID.Text.Trim(), this.hdnSubstituteSkuID.Value, "SUBSTITUTE_SKU") as SubstituteSkuInfo;
            if (substituteSku != null)
            {
                this.txtSubSkuID.Text = substituteSku.SubstituteSkuID;
                this.txtSubSkuID.Enabled = false;
                this.txtPack.Text = substituteSku.PackID;
                this.txtSubPack.Text = substituteSku.SubPackID;
                this.BindData2UomList(this.txtPack.Text.Trim(), "1");
                this.BindData2UomList(this.txtSubPack.Text.Trim(), "2");
                this.txtQty.Text = base.GetPreferenceDigit(substituteSku.Qty);
                this.txtSubQty.Text = base.GetPreferenceDigit(substituteSku.SubQty);
                this.txtSkuMainUomQty.Text = base.GetPreferenceDigit(substituteSku.UomQty);
                this.txtSubSkuMainSkuQty.Text = base.GetPreferenceDigit(substituteSku.SubUomQty);
                this.chkIsActive.Checked = substituteSku.IsActive == "Y";
                this.txtUdf1.Text = substituteSku.Udf1;
                this.txtUdf2.Text = substituteSku.Udf2;
                this.txtUdf3.Text = substituteSku.Udf3;
                this.txtUdf4.Text = substituteSku.Udf4;
                this.txtUdf5.Text = substituteSku.Udf5;
            }
        }

        protected void txtPack_TextChanged(object sender, EventArgs e)
        {
            this.BindData2UomList(this.txtPack.Text.Trim(), "1");
        }

        protected void txtSkuID_TextChanged(object sender, EventArgs e)
        {
            SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtOwnerID.Text, this.txtSkuID.Text, "", "SKU") as SkuInfo;
            if (skuInfo != null)
            {
                this.txtPack.Text = skuInfo.PackID;
                this.txtPack_TextChanged(null, null);
                this.txtPack.Enabled = false;
            }
        }

        protected void txtSubPack_TextChanged(object sender, EventArgs e)
        {
            this.BindData2UomList(this.txtSubPack.Text.Trim(), "2");
        }

        protected void txtSubSkuID_TextChanged(object sender, EventArgs e)
        {
            SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.txtOwnerID.Text, this.txtSubSkuID.Text, "", "SKU") as SkuInfo;
            if (skuInfo != null)
            {
                this.txtSubPack.Text = skuInfo.PackID;
                this.txtSubPack_TextChanged(null, null);
                this.txtSubPack.Enabled = false;
            }
        }
    }
}

